package com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom;

import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ChiefClubDO;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.ChiefClubRelationDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ChiefClubMapper {

    String TEMPLATE_RESULT_MAP = "chief_club_relation";

    @Select("select * from chief_club_relation where chief_id = #{chiefId}")
    @Results(id = TEMPLATE_RESULT_MAP,
            value = {
                    @Result(column = "id", property = "id"),
                    @Result(column = "chief_id", property = "chiefId"),
                    @Result(column = "club_id", property = "clubId"),
                    @Result(column = "create_by", property = "createBy"),
                    @Result(column = "update_by", property = "updateBy"),
                    @Result(column = "create_time", property = "createTime"),
                    @Result(column = "update_time", property = "updateTime")
            })
    List<ChiefClubDO> queryByChiefId(@Param("chiefId") long chiefId);

    @Insert("<script>" +
            "insert into chief_club_relation (`chief_id`,`club_id`,`create_by`,`update_by`) " +
            "values " +
            "<foreach collection='chiefClubDOList' item='item' separator=',' >" +
            "      (#{item.chiefId},#{item.clubId},#{item.createBy},#{item.updateBy})" +
            "    </foreach>" +
            "</script>")
    void batchInsert(@Param("chiefClubDOList") List<ChiefClubDO> chiefClubDOList);

    @Delete("<script>" +
            "delete from chief_club_relation " +
            "where `chief_id` = #{chiefId} " +
            "and `club_id` in " +
            "<foreach collection='clubIds' item='item' separator=',' open='(' close=')' >" +
            "      #{item}" +
            "</foreach>" +
            "</script>")
    void batchRemove(@Param("chiefId") long chiefId, @Param("clubIds") List<Long> clubIds);


    @Select("<script> select" +
            "\t user_club_meta.`name`," +
            "\t chief_club_relation.id," +
            "\t chief_club_relation.chief_id," +
            "\t chief_club_relation.club_id " +
            "FROM chief_club_relation " +
            "\t LEFT JOIN user_club_meta ON chief_club_relation.club_id = user_club_meta.id" +
            "\t <where>" +
            "<if test='clubId!=null and clubId >0'>" +
            "\t and chief_club_relation.club_id = #{clubId} " +
            "</if>" +
            "<if test='chiefId!=null and chiefId >0'>" +
            "\t and chief_club_relation.chief_id = #{chiefId} " +
            "</if>" +
            "</where>" +
            "\t order by chief_club_relation.update_time desc" +
            "\t limit #{offset},#{pageSize}" +
            "</script>")
    @Results(value = {
            @Result(column = "id", property = "id"),
            @Result(column = "chief_id", property = "chiefId"),
            @Result(column = "club_id", property = "clubId"),
            @Result(column = "name", property = "clubName")
    })
    List<ChiefClubRelationDO> queryByClubIdAndChiefId(@Param("clubId") Long clubId, @Param("chiefId") Long chiefId, @Param("offset") int offset, @Param("pageSize") int pageSize);


    @Delete("<script>" +
            "delete from chief_club_relation " +
            "where chief_club_relation.`id` = #{chiefClubRelationId} " +
            "</script>")
    void removeChiefClubRelation(@Param("chiefClubRelationId") long chiefClubRelationId);


    @Select("<script> select " +
            "count(chief_club_relation.club_id) " +
            "FROM chief_club_relation " +
            "LEFT JOIN user_club_meta ON chief_club_relation.club_id = user_club_meta.id " +
            "<where> " +
            "<if test='clubId!=null and clubId >0'> " +
            "and chief_club_relation.club_id = #{clubId} " +
            "</if> " +
            "<if test='chiefId!=null and chiefId >0'> " +
            "and chief_club_relation.chief_id = #{chiefId} " +
            "</if> " +
            "</where> " +
            "</script>")
    int count(@Param("clubId") Long clubId, @Param("chiefId") Long chiefId);

    @Update("<script> update " +
            "chief_club_relation " +
            "set update_by = #{updateBy} ," +
            "club_id = #{clubId} ," +
            "chief_id = #{chiefId} " +
            "where id = #{chiefClubRelationId} " +
            "limit 1" +
            "</script>")
    void updateChiefClubRelation(@Param("chiefClubRelationId") long chiefClubRelationId, @Param("chiefId") long chiefId, @Param("clubId") long clubId, @Param("updateBy") String updateBy);
}
